package sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @ClassName : SingleMySql
 * @Description : SingleMySql
 * @Author : ubing
 * @Date: 2022-12-04 17:04
 */
public class SingleMySql {
	//volatile 原子化，防止cpu加载重排序 从而导致空指针异常
	private static volatile SingleMySql singleMySql;

	static final String JDBC_DRIVER="com.mysql.cj.jdbc.Driver";

	static String DB_URL = null;
	static String DB_BASE=null;
	static String USER = null;
	static String PASS = null;
	public static Connection conn =null;
	public static Statement stmt = null;
	private SingleMySql(String url,String user,String pass) {
		DB_URL=url;
		USER=user;
		PASS=pass;
		try {
			Class.forName(JDBC_DRIVER);
			System.out.println("加载数据库驱动成功");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(url, user, pass);
			System.out.println("连接数据库成功");
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static SingleMySql getInstance(String url,String user,String pass){
		//双重验证
		if (singleMySql == null) {
			synchronized (SingleMySql.class) {
				if (singleMySql == null) {
					singleMySql = new SingleMySql(url,user,pass);
				}
			}
		}
		return singleMySql;
	}


}
